package com.ly.airline.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.airline.pojo.Liuyan;
import com.ly.airline.pojo.User;
import com.ly.airline.service.LiuyanService;
import com.ly.airline.utils.HttpResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ：linye
 * @ClassName LiuyanController
 * @date ：Created in 2022/4/28 20:12
 * @description： 留言接口
 * @modified By：
 * @version: 1.0
 */
@Controller
@RequestMapping("liuyan")
public class LiuyanController {

    @Autowired
    private LiuyanService liuyanService;

    /**
     *@param current
	* @param pageSize
	* @param model
     * @return java.lang.String
     * @author linye
     * @description: 后台获取留言列表
     * @date 2022/4/30 0:26
     */
    @GetMapping("getLiuyanList/{current}/{pageSize}")
    public String getLiuyanList(@PathVariable Integer current, @PathVariable Integer pageSize, Model model){
        IPage page = new Page(current,pageSize);
        liuyanService.page(page);
        model.addAttribute("pagers",page);
        return "admin/liuyan/liuyanMana";
    }

    /**
     *@param current
	* @param pageSize
	* @param model
     * @return java.lang.String
     * @author linye
     * @description: 前台获取留言列表
     * @date 2022/4/30 0:26
     */
    @GetMapping("getAllLiuyan/{current}/{pageSize}")
    public String getAllLiuyan(@PathVariable Integer current, @PathVariable Integer pageSize, Model model){
        IPage page = new Page(current,pageSize);
        liuyanService.page(page);
        model.addAttribute("pagers",page);
        return "qiantai/liuyan/liuyanAll";
    }

    /**
     *@param id
	* @param model
     * @return java.lang.String
     * @author linye
     * @description: 前台获取留言详情
     * @date 2022/4/30 0:26
     */
    @GetMapping("getLiuyanDetail/{id}")
    public String getLiuyanDetail(@PathVariable Integer id,Model model){
        Liuyan liuyan = liuyanService.getById(id);
        model.addAttribute("liuyan",liuyan);
        return "qiantai/liuyan/liuyanDetail";
    }

    /**
     *@param id
     * @return com.ly.airline.utils.HttpResult
     * @author linye
     * @description: 删除留言
     * @date 2022/4/30 0:26
     */
    @GetMapping("deleteLiuyan/{id}")
    @ResponseBody
    public HttpResult deleteLiuyan(@PathVariable Integer id){
        liuyanService.removeById(id);
        return HttpResult.success(true,"删除成功");
    }

    /**
     *@param id
	* @param model
     * @return java.lang.String
     * @author linye
     * @description: 后台跳转到留言回复页面
     * @date 2022/4/30 0:26
     */
    @GetMapping("toAddLiuyan/{id}")
    public String toAddLiuyan(@PathVariable Integer id,Model model){
        model.addAttribute("liuyanId",id);
        return "admin/liuyan/liuyanHuifu";
    }

    /**
     *@param
     * @return java.lang.String
     * @author linye
     * @description: 前台跳转到编写留言页面
     * @date 2022/4/30 0:26
     */
    @GetMapping("toAddFrontLiuyan")
    public String toAddFrontLiuyan(){
        return "qiantai/liuyan/liuyanAdd";
    }

    /**
     *@param liuyan
	* @param request
     * @return com.ly.airline.utils.HttpResult
     * @author linye
     * @description: 前台添加留言
     * @date 2022/4/30 0:26
     */
    @PostMapping("addFrontLiuyan")
    @ResponseBody
    public HttpResult addFrontLiuyan(Liuyan liuyan, HttpServletRequest request){
        if (Strings.isBlank(liuyan.getNeirong())){
            return HttpResult.success(false,"回复内容不能为空");
        }
        User user = (User) request.getSession().getAttribute("user");
        liuyan.setUserid(user.getUserId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        liuyan.setLiuyanshi(simpleDateFormat.format(new Date()));
        liuyanService.save(liuyan);
        return HttpResult.success(true,"留言成功");
    }

    /**
     *@param liuyan
     * @return com.ly.airline.utils.HttpResult
     * @author linye
     * @description: 后台添加留言回复
     * @date 2022/4/30 0:26
     */
    @PostMapping("addLiuyan")
    @ResponseBody
    public HttpResult addLiuyan(@RequestBody Liuyan liuyan){
        if (Strings.isBlank(liuyan.getHuifu())){
            return HttpResult.success(false,"回复内容不能为空");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        liuyan.setHuifushi(simpleDateFormat.format(new Date()));
        liuyanService.updateById(liuyan);
        return HttpResult.success(true,"回复成功");
    }
}
